/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.dataZx.service;

import com.fzhucc.modules.dataZx.domain.DataZx;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.dataZx.repository.DataZxRepository;
import com.fzhucc.modules.dataZx.domain.dto.DataZxDto;
import com.fzhucc.modules.dataZx.domain.dto.DataZxQueryCriteria;
import com.fzhucc.modules.dataZx.service.mapstruct.DataZxMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-03-29
**/
@Service
@RequiredArgsConstructor
public class DataZxService {

    private final DataZxRepository dataZxRepository;
    private final DataZxMapper dataZxMapper;

    public Map<String,Object> queryAll(DataZxQueryCriteria criteria, Pageable pageable){
        Page<DataZx> page = dataZxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dataZxMapper::toDto));
    }

    public List<DataZxDto> queryAll(DataZxQueryCriteria criteria){
        return dataZxMapper.toDto(dataZxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public DataZxDto findById(Integer id) {
        DataZx dataZx = dataZxRepository.findById(id).orElseGet(DataZx::new);
        ValidationUtil.isNull(dataZx.getId(),"DataZx","id",id);
        return dataZxMapper.toDto(dataZx);
    }

    @Transactional(rollbackFor = Exception.class)
    public DataZxDto create(DataZx resources) {
        return dataZxMapper.toDto(dataZxRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(DataZx resources) {
        DataZx dataZx = dataZxRepository.findById(resources.getId()).orElseGet(DataZx::new);
        ValidationUtil.isNull( dataZx.getId(),"DataZx","id",resources.getId());
        dataZx.copy(resources);
        dataZxRepository.save(dataZx);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dataZxRepository.deleteById(id);
        }
    }

    public void download(List<DataZxDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DataZxDto dataZx : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("值", dataZx.getValue());
            map.put("日期", dataZx.getDay());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}